package com.heima.model.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.mapper.ApArticleContentMapper;
import com.heima.model.article.mapper.ArticleMapper;
import com.heima.model.article.service.ArticleFreemarkerService;
import com.heima.model.article.service.ArticleService;
import com.heima.model.common.apArticle.dato.ArticleDto;
import com.heima.model.common.apArticle.dato.ArticleHomeDto;
import com.heima.model.common.apArticle.pojos.ApArticle;
import com.heima.model.common.apArticle.pojos.ApArticleConfig;
import com.heima.model.common.apArticle.pojos.ApArticleContent;
import com.heima.model.common.apArticle.pojos.ArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ApArticleConfigMapper articleConfigMapper;

    @Autowired
    private ApArticleContentMapper articleContentMapper;

    /**
     * 根据参数加载文章列表
     *
     * @param loadtype 1为加载更多  2为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //校验参数
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        dto.setSize(size);

        //类型参数
        if (!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_MORE) && !loadtype.equals(ArticleConstants.LOADTYPE_LOAD_NEW)) {
            loadtype = ArticleConstants.LOADTYPE_LOAD_MORE;
        }

        //文章频道校验
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //时间校验
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        //查询数据
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, loadtype);

        //结果封装
        ResponseResult responseResult = ResponseResult.okResult(apArticles);

        return responseResult;
    }

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    /**
     * 保存文章内容+文章配置+文章信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        if (dto.getContent() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = null;
        //如果没有id，则表达新增
        if (dto.getId() == null) {
            apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            articleMapper.insert(apArticle);

            //2.保存文章配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());

            articleConfigMapper.insert(apArticleConfig);
            //3.保存文章配置
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.insert(apArticleContent);
        } else {
            //修改文章+内容
            //1.先根据id查询文章，在修改
            apArticle = articleMapper.selectById(dto.getId());
            if (apArticle != null) {
                BeanUtils.copyProperties(dto, apArticle);
                //修改  文章
                articleMapper.updateById(apArticle);
                //先根据文章内容，在修改
                ApArticleContent apArticleContent = articleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
                apArticleContent.setContent(dto.getContent());
                articleContentMapper.updateById(apArticleContent);
            }
        }
        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());
        ResponseResult responseResult = ResponseResult.okResult(apArticle == null ? "" : apArticle.getId());

        return responseResult;
    }


}
